package com.ruoyi.unit.controller;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.unit.domain.BdUnit;
import com.ruoyi.unit.service.IBdUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 计量单位Controller
 *
 * @author ruoyi
 * @date 2023-11-14
 */
@Api(tags="计量单位管理")
@RestController
@RequestMapping("/unit/unit")
public class BdUnitController extends BaseController
{
    @Autowired
    private IBdUnitService bdUnitService;

    /**
     * 查询计量单位列表
     */
    @ApiOperation("查询计量单位列表")
    @PreAuthorize("@ss.hasPermi('unit:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(BdUnit bdUnit)
    {
        startPage();
        List<BdUnit> list = bdUnitService.selectBdUnitList(bdUnit);
        return getDataTable(list);
    }

    /**
     * 导出计量单位列表
     */
    @ApiOperation("导出计量单位列表")
    @PreAuthorize("@ss.hasPermi('unit:unit:export')")
    @Log(title = "计量单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdUnit bdUnit)
    {
        List<BdUnit> list = bdUnitService.selectBdUnitList(bdUnit);
        ExcelUtil<BdUnit> util = new ExcelUtil<BdUnit>(BdUnit.class);
        util.exportExcel(response, list, "计量单位数据");
    }

    /**
     * 获取计量单位详细信息
     */
    @ApiOperation("获取计量单位详细信息")
    @PreAuthorize("@ss.hasPermi('unit:unit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bdUnitService.getById(id));
    }

    /**
     * 新增计量单位
     */
    @ApiOperation("新增计量单位")
    @PreAuthorize("@ss.hasPermi('unit:unit:add')")
    @Log(title = "计量单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BdUnit bdUnit)
    {
        return toAjax(bdUnitService.save(bdUnit));
    }

    /**
     * 修改计量单位
     */
    @ApiOperation("修改计量单位")
    @PreAuthorize("@ss.hasPermi('unit:unit:edit')")
    @Log(title = "计量单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BdUnit bdUnit)
    {
        return toAjax(bdUnitService.updateById(bdUnit));
    }

    /**
     * 删除计量单位
     */
    @ApiOperation("删除计量单位")
    @PreAuthorize("@ss.hasPermi('unit:unit:remove')")
    @Log(title = "计量单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bdUnitService.removeByIds(Arrays.asList(ids)));
    }
}
